package com.eightywork.life.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.eightywork.life.R;
import com.eightywork.life.activity.PmDetailActivity;
import com.eightywork.life.imageCache.ImageLoader;
import com.eightywork.life.model.Pm;
import com.eightywork.life.service.DataService;

public class PmListAdapter extends ArrayAdapter<Pm> {
	private LayoutInflater pInflater;
	private Context pcontext;
	private int pResource  = R.layout.item_pm;
	private ImageLoader imageLoader;
	private List<Pm> list;
	public PmListAdapter(Context context, List<Pm> list) {
		super(context, 0, list);
		// TODO Auto-generated constructor stub
		pcontext = context;
		this.list = list;
		pInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);	
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return (list != null) ? list.size() : 0;
	}


	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		// TODO Auto-generated method stub
		View layout = convertView;
		if (layout == null) {
			layout = pInflater.inflate(pResource, parent, false);
		}
		final Pm pm = getItem(position);
		ImageView avatar = (ImageView)layout.findViewById(R.id.authorvavatar);
		ImageView verifytype = (ImageView)layout.findViewById(R.id.verify_type);
		verifytype.setBackgroundResource(DataService.getVerifyTypeRes(pm.getToverify_type()));
		imageLoader.DisplayImage(pm.getToavatar(), avatar, 0, 0, 0);
		TextView name = (TextView)layout.findViewById(R.id.authorname);
		name.setText(pm.getToname());
		TextView content = (TextView)layout.findViewById(R.id.msg);
		content.setText(pm.getMessage());
        final View label = (View)layout.findViewById(R.id.pm_label);
		if (pm.getIsnew().equals("1")) {
			label.setVisibility(View.VISIBLE);
		} else {
			label.setVisibility(View.GONE);
		}
		layout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				label.setVisibility(View.GONE);
				Intent intent = new Intent(pcontext, PmDetailActivity.class);
				intent.putExtra("touid", pm.getTouid());				
				intent.putExtra("toavatar", pm.getToavatar());
				intent.putExtra("toverify_type", pm.getToverify_type());
				pcontext.startActivity(intent);
			}
		});
		return layout;
	}

}
